using UnityEngine;

public class CrystalSkill : MonoBehaviour
{
    private static readonly int Explode = Animator.StringToHash("Explode");
    private Player player;
    private Animator Anim => GetComponent<Animator>();
    private CircleCollider2D circleCollider;
    private float exitTime;
    private bool canExplode = true;
    private bool canMoveToEnemy;
    private float moveSpeed;
    private bool canGrow;
    [SerializeField] private float growSpeed = 5;
    private Transform closestEnemyTransform;
    [SerializeField] private LayerMask whatIsEnemy;
    private BlackholeSkillController blackholeSkillController;

    private void Awake()
    {
        blackholeSkillController = FindObjectOfType<BlackholeSkillController>();
        circleCollider = GetComponent<CircleCollider2D>();
    }

    private void Update()
    {
        exitTime -= Time.deltaTime;
        if (exitTime <= 0)
        {
            Completed();
        }
        if (canMoveToEnemy && closestEnemyTransform)
        {
            transform.position =
                Vector2.MoveTowards(transform.position, closestEnemyTransform.position, moveSpeed * Time.deltaTime);
            if (Vector2.Distance(transform.position, closestEnemyTransform.position) < 1f)
            {
                Completed();
                canMoveToEnemy = false;
            }
        }
        if (canGrow)
        {
            transform.localScale = Vector2.Lerp(transform.localScale, new Vector3(3, 3), growSpeed * Time.deltaTime);
        }
    }

    public void Setup(float duration, bool canExplode, bool canMoveToEnemy, float moveSpeed, Transform closestEnemyTransform,
        Player player)
    {
        exitTime = duration;
        this.player = player;
        this.canExplode = canExplode;
        this.canMoveToEnemy = canMoveToEnemy;
        this.moveSpeed = moveSpeed;
        this.closestEnemyTransform = closestEnemyTransform;
    }

    public void ChooseRandomEnemy()
    {
        Collider2D[] results = new Collider2D[10];
        var size = Physics2D.OverlapCircleNonAlloc(transform.position, blackholeSkillController.GetRadius(), results,
            whatIsEnemy);
        if (size > 0)
        {
            closestEnemyTransform = results[Random.Range(0, size)].transform;
        }
    }

    private void AnimationExplodeEvent()
    {
        Collider2D[] results = new Collider2D[10];
        var size = Physics2D.OverlapCircleNonAlloc(transform.position, circleCollider.radius, results);
        for (int i = 0; i < size; i++)
        {
            if (!results[i].TryGetComponent(out Enemy enemy)) continue;
            enemy.SetupKnockbackDirection(transform.position);
            player.Stats.DoMagicalDamage(enemy.Stats);
            ItemDataEquipment equipedAmulet = player.Inventory.GetEquipment(EquipmentType.Amulet);
            if (equipedAmulet)
            {
                equipedAmulet.Effect(results[i].transform, player);
            }
        }
    }

    public void Completed()
    {
        if (canExplode)
        {
            canGrow = true;
            Anim.SetTrigger(Explode);
        }
        else
        {
            SelfDestroy();
        }
    }

    public void SelfDestroy()
    {
        Destroy(gameObject);
    }
}